﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYFamilyLibDataManager;

namespace YArchitech.HVAC
{
	public partial class AccessoryMoreForm : System.Windows.Forms.Form
	{
		[DllImport("User32.dll")]
		private static extern int SendMessage(int Handle, int wMsg, int wParam, int lParam);

		public static void SetListViewSpacing(ListView lst, int x, int y)
		{
			AccessoryMoreForm.SendMessage(lst.Handle.ToInt32(), 4149, 0, x * 65536 + y);
		}

		public AccessoryMoreForm()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
		}

		public AccessoryMoreForm(Autodesk.Revit.DB.Document doc, string accessoryType, string chooseType)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.Doc = doc;
			this.AccessoryType = accessoryType;
			this.ChooseType = chooseType;
			string accessoryType2 = this.AccessoryType;
			if (!(accessoryType2 == "duct"))
			{
				if (!(accessoryType2 == "pipe"))
				{
					if (!(accessoryType2 == "pump"))
					{
						if (!(accessoryType2 == "PipeAccessories"))
						{
							if (!(accessoryType2 == "GPipeAccessories"))
							{
								if (accessoryType2 == "PPipeAccessories")
								{
									this.Text = "排水附件布置-族立得";
									this.groupBox1.Text = "排水附件图示列表";
									this.groupBox2.Text = "排水附件名称列表";
									if (this.plumbDR == null)
									{
										this.plumbDR = new PlumbingDataReader(this.Doc);
									}
									this.treeView = this.plumbDR.GetFamilyAllInfoTree("排水附件");
								}
							}
							else
							{
								this.Text = "给水附件布置-族立得";
								this.groupBox1.Text = "给水附件图示列表";
								this.groupBox2.Text = "给水附件名称列表";
								if (this.plumbDR == null)
								{
									this.plumbDR = new PlumbingDataReader(this.Doc);
								}
								this.treeView = this.plumbDR.GetFamilyAllInfoTree("给水附件");
							}
						}
						else
						{
							this.Text = "管道附件布置-族立得";
							this.groupBox1.Text = "管道附件图示列表";
							this.groupBox2.Text = "管道附件名称列表";
							if (this.plumbDR == null)
							{
								this.plumbDR = new PlumbingDataReader(this.Doc);
							}
							this.treeView = this.plumbDR.GetFamilyAllInfoTree("管道附件");
							if (this.hvacDR == null)
							{
								this.hvacDR = new HVACDataReader(this.Doc);
							}
							this.AddTreeNodes(ref this.treeView, this.hvacDR.GetFamilyAllInfoTree("管道附件"));
						}
					}
					else
					{
						this.Text = "选择水管阀件-族立得";
						this.groupBox1.Text = "水阀图示列表";
						this.groupBox2.Text = "水阀名称列表";
						this.btnOK.Text = "确定";
						if (this.hvacDR == null)
						{
							this.hvacDR = new HVACDataReader(this.Doc);
						}
						this.treeView = this.hvacDR.GetFamilyAllInfoTree("水管阀件");
						if (this.plumbDR == null)
						{
							this.plumbDR = new PlumbingDataReader(this.Doc);
						}
						this.AddTreeNodes(ref this.treeView, this.plumbDR.GetFamilyAllInfoTree("水管阀件"));
					}
				}
				else
				{
					if (this.ChooseType == "ConnectionSetting")
					{
						this.Text = "选择水管阀件-族立得";
						this.groupBox1.Text = "水阀图示列表";
						this.groupBox2.Text = "水阀名称列表";
						this.btnOK.Text = "确定";
					}
					else
					{
						this.Text = "水阀布置-族立得";
						this.groupBox1.Text = "水阀图示列表";
						this.groupBox2.Text = "水阀名称列表";
					}
					if (this.hvacDR == null)
					{
						this.hvacDR = new HVACDataReader(this.Doc);
					}
					this.treeView = this.hvacDR.GetFamilyAllInfoTree("水管阀件");
				}
			}
			else
			{
				this.Text = "风阀布置-族立得";
				this.groupBox1.Text = "风阀图示列表";
				this.groupBox2.Text = "风阀名称列表";
				if (this.hvacDR == null)
				{
					this.hvacDR = new HVACDataReader(this.Doc);
				}
				this.treeView = this.hvacDR.GetFamilyAllInfoTree("风管阀件");
			}
			this.InitTree();
			this.tvAccNameLst.Controls.Add(this.treeView);
			this.initTreeView(this.treeView);
			this.InitDetailView(this.treeView);
		}

		private void InitTree()
		{
			this.allTreeNode = new List<TreeNode>();
			foreach (object obj in this.treeView.Nodes)
			{
				TreeNode tn = (TreeNode)obj;
				this.allTreeNode.AddRange(TreeViewTools.GetAllChildNode(tn));
			}
			this.treeView.Nodes.Clear();
			foreach (TreeNode node in this.allTreeNode)
			{
				this.treeView.Nodes.Add(node);
			}
		}

		private void AddTreeNodes(ref TreeView tv, TreeView newTv)
		{
			foreach (object obj in newTv.Nodes)
			{
				TreeNode node = (TreeNode)obj;
				if (!tv.Nodes.Contains(node))
				{
					tv.Nodes.Add(node);
				}
			}
		}

		private void InitDetailView(TreeView tv)
		{
			List<TreeNode> list = new List<TreeNode>();
			foreach (object obj in tv.Nodes)
			{
				TreeNode tn = (TreeNode)obj;
				list.AddRange(TreeViewTools.GetAllChildNode(tn));
			}
			this.lsvDetail.Items.Clear();
			foreach (TreeNode treeNode in list)
			{
				if (treeNode.Tag is DbFamily)
				{
					DbFamily dbFamily = treeNode.Tag as DbFamily;
					this.lsvDetail.LargeImageList.Images.Add(dbFamily.Name, dbFamily.Image);
					ListViewItem listViewItem = new ListViewItem();
					listViewItem.Text = dbFamily.Name;
					listViewItem.Tag = dbFamily;
					listViewItem.ImageKey = dbFamily.Name;
					this.lsvDetail.Items.Add(listViewItem);
					AccessoryMoreForm.SetListViewSpacing(this.lsvDetail, this.lsvDetail.LargeImageList.ImageSize.Width + 30, this.lsvDetail.LargeImageList.ImageSize.Height + 10);
				}
			}
		}

		private void initTreeView(TreeView tv)
		{
			tv.Dock = DockStyle.Fill;
			tv.FullRowSelect = true;
			tv.AfterSelect += this.treeView_AfterSelect;
			tv.BeforeSelect += this.treeView_BeforeSelect;
			tv.NodeMouseDoubleClick += this.treeView_NodeMouseDoubleClick;
			if (tv.Nodes != null && tv.Nodes.Count > 0)
			{
				TreeNode tn = tv.Nodes[0];
				this.tnSelected = TreeViewTools.GetFirstNode(tn);
				tv.SelectedNode = this.tnSelected;
			}
		}

		protected void treeView_AfterSelect(object sender, TreeViewEventArgs e)
		{
			foreach (object obj in this.lsvDetail.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				if (listViewItem.Text == e.Node.Text)
				{
					listViewItem.Selected = true;
				}
				else
				{
					listViewItem.Selected = false;
				}
			}
		}

		protected void treeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.tnSelected != null)
			{
				this.tnSelected.BackColor = System.Drawing.Color.White;
				this.tnSelected.ForeColor = System.Drawing.Color.Black;
			}
		}

		protected void treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			if (e.Node.Nodes == null || e.Node.Nodes.Count < 1)
			{
				this.treeView.SelectedNode = e.Node;
				this.btnOK_Click(null, null);
			}
		}

		private void hytbSearch_TextChanged(object sender, EventArgs e)
		{
			this.treeView.Nodes.Clear();
			this.lsvDetail.Items.Clear();
			foreach (TreeNode treeNode in this.allTreeNode)
			{
				if (treeNode.Name.Contains(this.hytbSearch.Text.Trim()))
				{
					this.treeView.Nodes.Add((TreeNode)treeNode.Clone());
				}
			}
			this.initTreeView(this.treeView);
			this.InitDetailView(this.treeView);
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (!this.FamilyLoad())
			{
				return;
			}
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private bool FamilyLoad()
		{
			if (this.lvSelected == null)
			{
				YJKMessageBox.Information("请选择一个阀件或附件！！");
				return false;
			}
			DbFamily family = this.lvSelected.Tag as DbFamily;
			DbFamilySymbol firstFamilySymbol;
			if (this.hvacDR == null)
			{
				firstFamilySymbol = this.plumbDR.GetFirstFamilySymbol(family);
			}
			else
			{
				firstFamilySymbol = this.hvacDR.GetFirstFamilySymbol(family);
			}
			if (firstFamilySymbol == null)
			{
				YJKMessageBox.Information("读取族库失败！");
				return false;
			}
			this.fs = firstFamilySymbol.Create(this.Doc);
			if (this.fs == null)
			{
				throw new Exception("加载族失败");
			}
			return true;
		}

		private void lsvDetail_SelectedIndexChanged(object sender, EventArgs e)
		{
			foreach (object obj in this.lsvDetail.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				if (listViewItem.Selected)
				{
					this.isHave = true;
					if (this.treeView.SelectedNode != null)
					{
						this.tnSelected = this.treeView.SelectedNode;
						this.tnSelected.BackColor = System.Drawing.Color.White;
						this.tnSelected.ForeColor = System.Drawing.Color.Black;
					}
					this.treeView.SelectedNode = TreeViewTools.GetSelectedNode(this.treeView, listViewItem.Text);
					this.tnSelected = this.treeView.SelectedNode;
					this.tnSelected.BackColor = System.Drawing.Color.DodgerBlue;
					this.tnSelected.ForeColor = System.Drawing.Color.White;
					listViewItem.BackColor = System.Drawing.Color.DodgerBlue;
					listViewItem.ForeColor = System.Drawing.Color.White;
					listViewItem.EnsureVisible();
					break;
				}
				listViewItem.BackColor = System.Drawing.Color.White;
				listViewItem.ForeColor = System.Drawing.Color.Black;
			}
			if (!this.isHave && this.treeView.SelectedNode != null)
			{
				foreach (object obj2 in this.lsvDetail.Items)
				{
					ListViewItem listViewItem2 = (ListViewItem)obj2;
					if (listViewItem2.Text == this.treeView.SelectedNode.Text)
					{
						listViewItem2.Selected = true;
						listViewItem2.BackColor = System.Drawing.Color.DodgerBlue;
						listViewItem2.ForeColor = System.Drawing.Color.White;
						listViewItem2.EnsureVisible();
					}
					else
					{
						listViewItem2.Selected = false;
						listViewItem2.BackColor = System.Drawing.Color.White;
						listViewItem2.ForeColor = System.Drawing.Color.Black;
					}
				}
			}
			foreach (object obj3 in this.lsvDetail.Items)
			{
				ListViewItem listViewItem3 = (ListViewItem)obj3;
				if (listViewItem3.Selected)
				{
					this.lvSelected = listViewItem3;
				}
			}
		}

		private void lsvDetail_DoubleClick(object sender, EventArgs e)
		{
			this.btnOK_Click(null, null);
		}

		public FamilySymbol fs;

		private Document Doc;

		private string AccessoryType = string.Empty;

		private string ChooseType = string.Empty;

		private TreeView treeView;

		private ListViewItem lvSelected;

		private TreeNode tnSelected;

		private HVACDataReader hvacDR;

		private PlumbingDataReader plumbDR;

		public string strFamilyName = string.Empty;

		private List<TreeNode> allTreeNode;

		private const int LVM_FIRST = 4096;

		private const int LVM_SETICONSPACING = 4149;

		private bool isHave;
	}
}
